package app1.restTemplate

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
import grails.converters.JSON
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.web.client.RestTemplate

class ServiceSet1Proxy {

    @Autowired
    RestTemplate restTemplate

    @HystrixCommand(fallbackMethod = "reliable")
    Map sampleApi1(String bearerToken, String dependency) {
        def service_uri = "http://SERVICE-SET-1/sampleApi1?dependency={dependency}", args = [dependency: dependency]

        def requestHeaders = new HttpHeaders()
        requestHeaders.set("Authorization", "Bearer " + bearerToken)

        JSON.parse(restTemplate.exchange(service_uri, HttpMethod.GET, new HttpEntity<String>("parameters", requestHeaders), String.class, args).body)
    }

    Map reliable(String bearerToken, String dependency) {
        [token: bearerToken, provider: "sampleApi1@serviceSet1", dependency: dependency, state: "offline"]
    }
}
